package dyy.zc.note.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import cn.bmob.v3.datatype.BmobFile;
import dyy.zc.note.R;
import dyy.zc.note.bean.Classify;
import dyy.zc.note.bean.NoteInfo;
import dyy.zc.note.listener.OnItemClickListener;
import dyy.zc.note.utils.ImageLoaderUtils;
import dyy.zc.note.utils.Utils;


/**
 * Created by chuan on 2017/4/13.
 */

public class ClassifyAdapter extends  RecyclerView.Adapter<ClassifyAdapter.MyViewHolder>{
    private Context context;
    private List<String> list;
    private OnItemClickListener onItemClickListener;
    private List<NoteInfo> noteInfoList;

    public ClassifyAdapter(Context context, List<String> list,List<NoteInfo> noteInfoList){
        this.context = context;
        this.list = list;
        this.noteInfoList = noteInfoList;
    }

    public void setList(List<String> list,List<NoteInfo> noteInfoList) {
        this.list = list;
        this.noteInfoList = noteInfoList;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.item_classify,null),onItemClickListener);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.name.setText(list.get(position));
        int count = 0;
        for (NoteInfo n:noteInfoList){
            if (n.getType().equals(list.get(position))){
                count++;
            }
        }
        holder.count.setText(""+count+"");
    }

    @Override
    public int getItemCount() {
        if (list == null || list.size() == 0){
            return 0;
        }
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private OnItemClickListener onItemClickListener;
        private TextView name,count;
        public MyViewHolder(View itemView,OnItemClickListener onItemClickListener) {
            super(itemView);
            itemView.setOnClickListener(this);
            this.onItemClickListener = onItemClickListener;
            name = Utils.findViewsById(itemView,R.id.name);
            count = Utils.findViewsById(itemView,R.id.count);
        }

        @Override
        public void onClick(View v) {
            if (onItemClickListener !=null){
                onItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
}
